import axios from "axios"

const service = axios.create({
    baseURL:"http://localhost:9999",
    timeout:5000
})

service.interceptors.request.use(
    config=>{
        const token = localStorage.getItem("token")
        if(token){
            config.headers['Authorization'] = 'Bearer'+ ' ' + token
        }
        return config
    },
    err=>{
        console.log("requset error");
        return Promise.reject(err)
    }
)

service.interceptors.response.use(
    reponse=>{
        if(reponse.status === 200){
            const token = reponse.headers.authorization
            if(token){
                localStorage.setItem("token",token)
            }
            return Promise.resolve(reponse.data)
        }else{
            return Promise.reject(reponse.data)
        }
    },
    err=>{
        console.log("requset error");
        return Promise.reject(err)
    }
)

export default service